﻿using System;
using System.Data;
using UniMana.Utils.Data;
using UniMana.Data;
using UniMana.Utils.Controls;
using System.Linq;
using DevExpress.XtraBars;
using UniMana.Utils.Configs;
using UniMana.Utils.Helper;

namespace UniMana.Win.Personnel
{
    public partial class FrmContactWorkDays : DevExpress.XtraEditors.XtraForm
    {
        DBMain m_data = new DBMain();
        public FrmContactWorkDays()
        {
            InitializeComponent();
            lueCatalogue_Department.GetData(m_data.m_DCReadOnly.Catalogue_Departments);
        }

        private void lueCatalogue_Department_EditValueChanged_1(object sender, EventArgs e)
        {
            DataTable tbl = m_data.m_DCReadOnly.Personnel_ContactWorkDays.GetPersonelContactWorkDays(m_data, lueCatalogue_Department.GetIntValue()).ToDataTable();
            if (tbl.Rows.Count != 0)
            {
                gctAttendance.DataSource = tbl;
            }
            else
            {
                var q = from qr in m_data.m_DCReadOnly.Personnel_Personnels
                        where qr.Catalogue_DepartmentKey == lueCatalogue_Department.GetIntValue()
                        select new
                        {
                            qr.Personnel_PersonnelKey,
                            qr.LastName,
                            qr.Name,
                            ContactWorkDays =0
                        };
                
                gctAttendance.DataSource = q.ToDataTable();
            }
        }

        #region [XucBar - Events]
        //Xử lý sự kiện cho nút Save
        private void btnSave_Click(object sender, ItemClickEventArgs e)
        {
            this.Validate();
            try
            {
                DataTable tbl = (gctAttendance.DataSource as DataTable);
                foreach (DataRow r in tbl.Rows)
                {
                    string key = r["Personnel_PersonnelKey"].ToString();
                    Personnel_ContactWorkDaysInfo info = new Personnel_ContactWorkDaysInfo(key);
                    info.Properties.ContactWorkDays = float.Parse(r["ContactWorkDays"].ToString());
                    info.Save();
                }
                MsgConfig.SaveSuccessDialog();
            }
            catch { MsgConfig.SaveUnSuccessDialog(); }
        }

        private void btnPrint_Click(object sender, ItemClickEventArgs e)
        {
            gctAttendance.ShowPrintPreview();
        }
        #endregion
    }
}